home *** CD-ROM | disk | FTP | other *** search
Wrap
property pSprite, pCenterOffset, pLimits, pLimitsOrigin, pPath, pMovePeriod, pMoveStart, pMoveEnd, pRotate, pRotateStart, pRotatePeriod, pRotateEnd, pLimitsLeft, pLimitsRight, pLimitsTop, pLimitsBottom, pSpeed, pRotationSpeed, pLoopiness, pWackiness on getBehaviorDescription vDesc = "RANDOM MOVEMENT & ROTATION" & RETURN & RETURN vDesc = vDesc & "Sprite will move randomly within defined area, spinning" vDesc = vDesc && "wildly if so desired. Author can control both the speed of" vDesc = vDesc && "the movement and speed of rotation, as well as how radically" vDesc = vDesc && "the movement and rotation vary. Default movement limit" vDesc = vDesc && "is the stage area." & RETURN & RETURN vDesc = vDesc & "PERMITTED MEMBER TYPES:" & RETURN & mPermittedMemberTypes() vDesc = vDesc & RETURN & RETURN & "PARAMETERS:" & RETURN vDesc = vDesc & " - Horizontal and vertical coordinates for movement limits" vDesc = vDesc & RETURN & " - Average speed in pixels/second" & RETURN vDesc = vDesc & " - Loopiness modifies how close the sprite's path is to" vDesc = vDesc && "a straight line." & RETURN vDesc = vDesc & " - Rotation speed determines how fast sprite rotates;" vDesc = vDesc && "1000 = 360 degrees/second" & RETURN vDesc = vDesc & " - Wackiness controls how much the rotation of the" vDesc = vDesc & "sprite may vary before changing direction." return vDesc end on getBehaviorTooltip me vTip = "Animate a sprite along a random path" & RETURN vTip = vTip & "and determine its motion and speed" & RETURN vTip = vTip & "while it is animating." return vTip end on beginSprite me mInitialize(me) end on prepareFrame me mUpdate(me) end on mInitialize me pSprite = sprite(me.spriteNum) vMember = pSprite.member if not getPos(mPermittedMemberTypes(), vMember.type) then alert("Sprite" && pSprite.spriteNum & ": 'Random Movement & Rotation' works with" && mPermittedMemberTypes() && "cast members,") end if case vMember.type of #animgif, #flash, #quickTimeMedia, #digitalVideo, #vectorShape: if vMember.directToStage then alert("Sprite" && pSprite.spriteNum & ": Direct To Stage media may cause" && "playback problems with the 'Random Movement & Rotation' behavior.") end if end case if pRotationSpeed > 0 then case vMember.type of #field, #picture: pRotationSpeed = 0 alert("Sprite" && pSprite.spriteNum & ": 'Random Movement & Rotation' behavior cannot rotate #field or #picture" && "sprites. Set the Rotation Speed parameter for this sprite to 0" && "to prevent this dialog appearing") end case end if vRect = pSprite.rect vHalfHeight = vRect.height / 2 vHalfWidth = vRect.width / 2 vMaxDimension = max(vHalfHeight, vHalfWidth) vFarCorner = max(mVectorLength(pSprite.loc - point(vRect.left, vRect.top)), mVectorLength(pSprite.loc - point(vRect.right, vRect.top)), mVectorLength(pSprite.loc - point(vRect.left, vRect.bottom)), mVectorLength(pSprite.loc - point(vRect.right, vRect.bottom))) vCenter = point(vHalfWidth, vHalfHeight) + point(vRect.left, vRect.top) pCenterOffset = vCenter - pSprite.loc pLimits = rect(pLimitsLeft, pLimitsTop, pLimitsRight, pLimitsBottom) pLimits = pLimits + rect(vFarCorner, vFarCorner, -vFarCorner, -vFarCorner) if (pLimits.width < vRect.width) or (pLimits.height < vRect.height) then alert("Sprite" && pSprite.spriteNum & ": 'Random Movement & Rotation' behavior movement limitations" && "are too small to allow for full rotation of sprite.") end if pLimitsOrigin = point(pLimits.left, pLimits.top) mNewPath(me) mNewRotation(me) end on mUpdate me vTime = the milliSeconds mMove(me, vTime) mRotate(me, vTime) end on mMove me, vTime if pSpeed then if vTime < pMoveEnd then vElapsed = vTime - pMoveStart if vElapsed > 0 then vT1 = float(vElapsed) / pMovePeriod vT2 = vT1 * vT1 vT3 = vT2 * vT1 vNewPosition = pPath.p0 vModPoint = pPath.dc * vT1 vNewPosition = vNewPosition + vModPoint vModPoint = pPath.db * vT2 vNewPosition = vNewPosition + vModPoint vModPoint = pPath.da * vT3 vNewPosition = vNewPosition + vModPoint pSprite.loc = vNewPosition end if else pSprite.loc = pPath.p3 mNewPath(me) end if end if end on mRotate me, vTime if pRotationSpeed then if vTime < pRotateEnd then vElapsed = vTime - pRotateStart if vElapsed > 0 then vRotation = pRotate.start + (pRotate.diff * vElapsed / pRotatePeriod) pSprite.rotation = vRotation end if else pSprite.rotation = pRotate.end mNewRotation(me) end if end if end on mNewPath me if voidp(pPath) then pPath = [#p0: pSprite.loc, #p1: pSprite.loc, #p2: pSprite.loc, #p3: pSprite.loc] end if if pSpeed then vDest = point(random(pLimits.width), random(pLimits.height)) + pLimitsOrigin vP0 = pPath.p3 vVector = vDest - vP0 vVectorLen = mVectorLength(vVector) vLoopiness = vVectorLen * pLoopiness / 25 vP1 = mRestrain(vP0 + (pPath.p3 - pPath.p2), pLimits) if vLoopiness then vRandomPoint = point(mRandomSign() * random(vLoopiness), mRandomSign() * random(vLoopiness)) else vRandomPoint = point(0, 0) end if vP2 = mRestrain(vP0 + (vVector * 2 / 3) + vRandomPoint, pLimits) pPath = [#p0: vP0, #p1: vP1, #p2: vP2, #p3: vDest] setaProp(pPath, #dc, 3 * (pPath.p1 - pPath.p0)) setaProp(pPath, #db, (3 * (pPath.p2 - pPath.p1)) - pPath.dc) setaProp(pPath, #da, pPath.p3 - pPath.p0 - pPath.dc - pPath.db) vDistance = mVectorLength(pPath.p0 - pPath.p1) + mVectorLength(pPath.p1 - pPath.p2) + mVectorLength(pPath.p2 - pPath.p3) pMovePeriod = vDistance * 1000 / pSpeed pMoveStart = the milliSeconds pMoveEnd = pMoveStart + pMovePeriod end if end on mNewRotation me if voidp(pRotate) then pRotate = [#start: 0, #end: 0, #diff: 0] end if if pRotationSpeed then vRotation = pSprite.rotation if pRotate.diff < 0 then vOffset = random(pWackiness) else vOffset = -random(pWackiness) end if vTargetRotation = vRotation + vOffset pRotateStart = the milliSeconds pRotatePeriod = abs(vOffset) * 1000 / pRotationSpeed * 1000 / 360 pRotateEnd = pRotateStart + pRotatePeriod pRotate = [#start: vRotation, #end: vTargetRotation, #diff: vOffset] end if end on mVectorLength vVector vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV) return sqrt(vSquare) end on mRandomSign return (random(2) * 2) - 3 end on mRestrain vPoint, vRect vPoint.locH = max(vRect.left, min(vRect.right, vPoint.locH)) vPoint.locV = max(vRect.top, min(vRect.bottom, vPoint.locV)) return vPoint end on getPropertyDescriptionList me if not (the currentSpriteNum) then exit end if vRect = (the stage).rect vMemberType = sprite(the currentSpriteNum).member.type case vMemberType of #text, #picture: vRotateSpeed = 0 otherwise: vRotateSpeed = 100 end case vPDList = [:] setaProp(vPDList, #pLimitsLeft, [#comment: "Limit of movement (left)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.width]]) setaProp(vPDList, #pLimitsTop, [#comment: "Limit of movement (top)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.height]]) setaProp(vPDList, #pLimitsRight, [#comment: "Limit of movement (right)", #format: #integer, #default: vRect.width, #range: [#min: 0, #max: vRect.width]]) setaProp(vPDList, #pLimitsBottom, [#comment: "Limit of movement (bottom)", #format: #integer, #default: vRect.height, #range: [#min: 0, #max: (the stage).rect.height]]) setaProp(vPDList, #pSpeed, [#comment: "Speed of movement", #format: #integer, #default: 100, #range: [#min: 0, #max: 1000]]) setaProp(vPDList, #pLoopiness, [#comment: "Loopiness", #format: #integer, #default: 10, #range: [#min: 0, #max: 25]]) setaProp(vPDList, #pRotationSpeed, [#comment: "Speed of rotation", #format: #integer, #default: vRotateSpeed, #range: [#min: 0, #max: 1000]]) setaProp(vPDList, #pWackiness, [#comment: "Wackiness", #format: #integer, #default: 120, #range: [#min: 0, #max: 360]]) return vPDList end on mPermittedMemberTypes me return [#bitmap, #flash, #picture, #field, #text, #vectorShape, #animgif] end